/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ticketbooking;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.SealedObject;
import javax.crypto.SecretKey;

/**
 *
 * @author Kse7en
 */
public class Crypto {
        
        static String password = "mypassword";
        static String salt = "mysalt";
        static int keyLength = 256;
        
        static int keySize = 2048;
        
        public static SecretKey sessionKeyGenerator() throws Exception {
                return KeyManagement.PBE(password, salt, keyLength);
        }
        
        public static KeyPair KeyPairGenerator() throws Exception {
                return KeyManagement.KeyPairGenerator(keySize);
        }
        
        public static byte[] IV(SecretKey secretKey) throws Exception {
                return AES.IV(secretKey);
        }
        
        public static byte[] encryptTextWithAES(String plainText, SecretKey secretKey) throws Exception {
                
                return AES.encrypt(plainText, secretKey);        
        }
        
        public static SealedObject encryptObjectWithAES(Object plainObj, SecretKey secretKey) throws Exception {
                
                return AES.encryptObj(plainObj, secretKey);
        }
        
        public static String decryptTextWithAES(byte[] cipherText, SecretKey secretKey, byte[] iv) throws Exception {
                
                return AES.decrypt(cipherText, secretKey, iv);
        }
        
        public static Object decryptObjectWithAES(SealedObject EO, SecretKey secretKey, byte[] iv) throws Exception {
                
                return AES.decryptObj(EO, secretKey, iv);
        }
        
        public static byte[] encryptTextWithRSA(String plainText, PublicKey publicKey) throws Exception {
                
                return RSA.encrypt(plainText, publicKey);
        }
        
        public static SealedObject encryptObjectWithRSA(Object plainObj, PublicKey publicKey) throws Exception {
                
                return RSA.encryptObj(plainObj, publicKey);
        }
        
        public static String decryptTextWithRSA(byte[] cipherText, PrivateKey privateKey) throws Exception {
                
                return RSA.decrypt(cipherText, privateKey);
        }
        
        public static Object decryptObjectWithRSA(SealedObject SO, PrivateKey privateKey) throws Exception {
                
                return RSA.decryptObj(SO, privateKey);
        }
        
        public static void saveKeyPair(KeyPair kp) throws Exception {
                KeyManagement.saveKeyPair(kp);
        }
        
        public static KeyPair loadKeyPair() throws Exception {
                
                PublicKey publicKey = KeyManagement.readPublicKeyFromFile("public.key");
                PrivateKey privateKey = KeyManagement.readPrivateKeyFromFile("private.key");
                KeyPair kp = new KeyPair(publicKey, privateKey);
                
                return kp;
        }
        
        public static TransferMessageWithSign sign(TransferMessage TM, PrivateKey privateKey) throws Exception {
                
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                ObjectOutputStream os = new ObjectOutputStream(out);
                os.writeObject(TM);
                byte[] sign = DigitalSignature.sign(out.toByteArray(), privateKey);
              
                TransferMessageWithSign TMWS = new TransferMessageWithSign(TM, sign);
                return TMWS;
        }
        
        public static boolean verify(TransferMessageWithSign TMWS, PublicKey publicKey) throws Exception {
                
                byte[] sign = TMWS.getSign();
                TransferMessage TM = TMWS.getMessage();
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                ObjectOutputStream os = new ObjectOutputStream(out);
                os.writeObject(TM);
                
                boolean verify = DigitalSignature.verify(out.toByteArray(), publicKey, sign);
                
                return verify;
        }
  
}
